home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 October / Macformat17.cdr / Shareware City / Developers / WASTETCL / WASTEEdit ƒ / CEditApp.cp next >
Encoding:
Text File  |  1994-07-02  |  6.8 KB  |  300 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.     CEditApp.c
  3.     
  4.     Application methods for a tiny editor.
  5.         
  6.     Copyright © 1989 Symantec Corporation. All rights reserved.
  7.  
  8.  ******************************************************************************/
  9.  
  10. #include "Commands.h"
  11. #include "CBartender.h"
  12. #include "CEditApp.h"
  13. #include "CEditDoc.h"
  14. #include "Global.h"
  15. #include "WASTE.h"
  16. #ifndef __SCRIPT__
  17. #include <Script.h>
  18. #endif
  19.  
  20. #include "CTSMSwitchboard.h"
  21.  
  22. static Boolean    TSMAvailable(void);
  23.  
  24. short gUsingTSM = false;
  25.  
  26. extern    OSType        gSignature;
  27. extern    CBartender *gBartender;
  28.  
  29. #define        kExtraMasters        10
  30. #define        kRainyDayFund        45000
  31. #define        kCriticalBalance    40000
  32. #define        kToolboxBalance        20000
  33.  
  34.  
  35. /***
  36.  * IEditApp
  37.  *
  38.  *    Initialize the application. Your initialization method should
  39.  *    at least call the inherited method. If your application class
  40.  *    defines its own instance variables or global variables, this
  41.  *    is a good place to initialize them.
  42.  *
  43.  ***/
  44.  
  45. void CEditApp::IEditApp(void)
  46.  
  47. {
  48.     gUsingTSM = TSMAvailable();
  49.     if (gUsingTSM) gUsingTSM = (InitTSMAwareApplication()==noErr); // can we init?
  50.  
  51.     CApplication::IApplication( kExtraMasters, kRainyDayFund,
  52.                         kCriticalBalance, kToolboxBalance);
  53.  
  54. }
  55.  
  56.  
  57.  
  58. /***
  59.  * SetUpFileParameters
  60.  *
  61.  *    In this routine, you specify the kinds of files your
  62.  *    application opens.
  63.  *
  64.  *
  65.  ***/
  66.  
  67. void CEditApp::SetUpFileParameters(void)
  68.  
  69. {
  70.     inherited::SetUpFileParameters();    /* Be sure to call the default method */
  71.  
  72.         /**
  73.          **    sfNumTypes is the number of file types
  74.          **    your application knows about.
  75.          **    sfFileTypes[] is an array of file types.
  76.          **    You can define up to 4 file types in
  77.          **    sfFileTypes[].
  78.          **
  79.          **/
  80.  
  81.     sfNumTypes = 1;
  82.     sfFileTypes[0] = 'TEXT';
  83.  
  84.         /**
  85.          **    Although it's not an instance variable,
  86.          **    this method is a good place to set the
  87.          **    gSignature global variable. Set this global
  88.          **    to your application's signature. You'll use it
  89.          **    to create a file (see CFile::CreateNew()).
  90.          **
  91.          **/
  92.  
  93.     gSignature = '???\?';
  94. }
  95.  
  96.  
  97. /***
  98.  * SetUpMenus
  99.  *
  100.  *    In this method, you add special menu items and set the
  101.  *    menu item dimming and checking options for your menus.
  102.  *    The most common special menu items are the names of the
  103.  *    fonts. For this tiny editor, you also want to set up the
  104.  *    dimming and checking options so only the current font
  105.  *    and size are checked.
  106.  *
  107.  ***/
  108.  
  109. void CEditApp::SetUpMenus(void)
  110.  
  111. {
  112.         /**
  113.          ** Let the default method read the menus from
  114.          **    the MBAR 1 resource.
  115.          **
  116.          **/
  117.  
  118.     inherited::SetUpMenus();
  119.  
  120.         /**
  121.          ** Add the fonts in the  system to the
  122.          **    Font menu. Remember, MENUfont is one
  123.          **    of the reserved font numbers.
  124.          **
  125.          **/
  126.  
  127.     AddResMenu(GetMHandle(MENUfont), 'FONT');
  128.  
  129.         /**
  130.          **    The UpdateMenus() method sets up the dimming
  131.          **    for menu items. By default, the bartender dims
  132.          **    all the menus, and each bureaucrat is reponsible
  133.          **    for turning on the items that correspond to the commands
  134.          **    it can handle.
  135.          **
  136.          **    Set up the options here. The edit pane's UpdateMenus()
  137.          **    method takes care of doing the work.
  138.          **
  139.          **    For Font and Size menus, you want all the items to
  140.          **    be enabled all the time. In other words, you don't
  141.          **    want the bartender to ever dim any of the items
  142.          **    in these two menus.
  143.          **
  144.          **/
  145.  
  146.     gBartender->SetDimOption(MENUfont, dimNONE);
  147.     gBartender->SetDimOption(MENUsize, dimNONE);
  148.     gBartender->SetDimOption(MENUstyle, dimNONE);
  149.  
  150.         /**
  151.          **    For Font and Size menus, one of the items
  152.          **    is always checked. Setting the unchecking option
  153.          **    to TRUE lets the bartender know that it should
  154.          **    uncheck all the menu items because an UpdateMenus()
  155.          **    method will check the right items.
  156.          **    For the Style menu, uncheck all the items and
  157.          **    let the edit pane's UpdateMenus() method check the
  158.          **    appropriate ones.
  159.          **
  160.          **/
  161.  
  162.     gBartender->SetUnchecking(MENUfont, TRUE);
  163.     gBartender->SetUnchecking(MENUsize, TRUE);
  164.     gBartender->SetUnchecking(MENUstyle, TRUE);
  165. }
  166.  
  167.  
  168.  
  169. /***
  170.  * CreateDocument
  171.  *
  172.  *    The user chose New from the File menu.
  173.  *    In this method, you need to create a document and send it
  174.  *    a NewFile() message.
  175.  *
  176.  ***/
  177.  
  178. void CEditApp::CreateDocument()
  179.  
  180. {
  181.     CEditDoc    *theDocument = NULL;
  182.     
  183.     // In the event that creating the document fails,
  184.     // we setup an exception handler here. If any
  185.     // of the methods called within the scope of this
  186.     // TRY block fail, an exception will be raised and
  187.     // control will be transferred to the CATCH block.
  188.     // Here, the CATCH block takes care of disposing
  189.     // of the partially created document.
  190.     
  191.     TRY
  192.     {
  193.         theDocument = new(CEditDoc);
  194.             
  195.             /**
  196.              **    Send your document an initialization
  197.              **    message. The first argument is the
  198.              **    supervisor (the application). The second
  199.              **    argument is TRUE if the document is printable.
  200.              **
  201.              **/
  202.         
  203.         theDocument->IEditDoc(this, TRUE);
  204.     
  205.             /**
  206.              **    Send the document a NewFile() message.
  207.              **    The document will open a window, and
  208.              **    set up the heart of the application.
  209.              **
  210.              **/
  211.         theDocument->NewFile();
  212.     }
  213.     CATCH
  214.     {
  215.         ForgetObject( theDocument);
  216.     }
  217.     ENDTRY;
  218. }
  219.  
  220. /***
  221.  * OpenDocument
  222.  *
  223.  *    The user chose Open… from the File menu.
  224.  *    In this method you need to create a document
  225.  *    and send it an OpenFile() message.
  226.  *
  227.  *    The macSFReply is a good SFReply record that contains
  228.  *    the name and vRefNum of the file the user chose to
  229.  *    open.
  230.  *
  231.  ***/
  232.  
  233. void CEditApp::OpenDocument(SFReply *macSFReply)
  234.  
  235. {
  236.     CEditDoc    *theDocument = NULL;
  237.  
  238.     // In the event that opening the document fails,
  239.     // we setup an exception handler here. If any
  240.     // of the methods called within the scope of this
  241.     // TRY block fail, an exception will be raised and
  242.     // control will be transferred to the CATCH block.
  243.     // Here, the CATCH block takes care of disposing
  244.     // of the partially opened document.
  245.  
  246.     TRY
  247.     {    
  248.         theDocument = new(CEditDoc);
  249.             
  250.             /**
  251.              **    Send your document an initialization
  252.              **    message. The first argument is the
  253.              **    supervisor (the application). The second
  254.              **    argument is TRUE if the document is printable.
  255.              **
  256.              **/
  257.         
  258.         theDocument->IEditDoc(this, TRUE);
  259.     
  260.             /**
  261.              **    Send the document an OpenFile() message.
  262.              **    The document will open a window, open
  263.              **    the file specified in the macSFReply record,
  264.              **    and display it in its window.
  265.              **
  266.              **/
  267.         theDocument->OpenFile(macSFReply);
  268.     }
  269.     CATCH
  270.     {
  271.         ForgetObject( theDocument);
  272.     }
  273.     ENDTRY;
  274. }
  275.  
  276. /*** Dispose added for WASTE support ***/
  277. void CEditApp::Dispose(void)
  278. {
  279.         if (gUsingTSM) CloseTSMAwareApplication();
  280. }
  281.  
  282. /*** make TSMSwitchboard ***/
  283. void CEditApp::MakeSwitchboard( void)
  284. {
  285.     if (gSystem.hasAppleEvents && gUsingTSM)
  286.     {
  287.         FailOSErr(WEInstallTSMHandlers());
  288.     }
  289.     itsSwitchboard = (CSwitchboard *)new CTSMSwitchboard();
  290.     itsSwitchboard->InitAppleEvents(); // needed for TCL 2.0.3
  291. }
  292.  
  293.  
  294. static Boolean TSMAvailable(void)
  295. {
  296.     long    response;
  297.     
  298.     return (Gestalt(gestaltTSMgrVersion, &response)==noErr);
  299. }
  300.